﻿/**
* @author amalafe
*/

Namespace("sketch.framework");
(function () {
    var fwk = sketch.framework;

    fwk.Rectangle = klass(fwk.UIElement, {
        _constructor : function(){
            this.properties.createProperty("fillStyle", "Fill style", null);
            this.properties.createProperty("fillResourceGradient", "Fill resource gradient", null);
            this.properties.createProperty("strokeStyle", "Stroke style", null);
            this.properties.createProperty("cornerRadius", "Corner radius", 0);
        },

        fillStyle: function(value){
            return this.properties.fillStyle(value);
        },
        strokeStyle: function(value){
            return this.properties.strokeStyle(value);
        },
        cornerRadius: function(value){
            return this.properties.cornerRadius(value);
        },
        fillResourceGradient: function(value){
            return this.properties.fillResourceGradient(value);
        },

        drawSelf : function(context) {
            var l = this.left(),
                t = this.top(),
                w = this.width(),
                h = this.height();

            var cornerRadius = this.cornerRadius();
            var hasCornerRadius = cornerRadius > 0;

            context.save();

            var fillStyle = this.fillStyle();
            var strokeStyle = this.strokeStyle();
            var fillGradient = this.fillResourceGradient();
            var hasFill = fillStyle || fillGradient;

            if (fillStyle){
                context.fillStyle = fillStyle;
            }
            if (fillGradient){
                context.fillStyle = fwk.Resources[fillGradient].toBrush(context, l, t, l, t + h);
            }
            if (strokeStyle){
                context.strokeStyle = strokeStyle;
            }

            if (hasCornerRadius){
                context.roundedRectPath(l, t, w, h, cornerRadius, cornerRadius);
                if (hasFill){
                    context.fill();
                }
                if (strokeStyle){
                    context.stroke();
                }
            }
            else {
                if (hasFill) {
                    context.fillRect(l, t, w, h);
                }
                if (strokeStyle){
                    context.strokeRect(l, t, w, h);
                }
            }

            context.restore();
        }
    });

    fwk.Rectangle.create = function(x, y, w, h, fillStyle, strokeStyle){
        var rect = new fwk.Rectangle();
        if(arguments.length == 4){
            rect.left(x);
            rect.top(y);
            rect.width(w);
            rect.heigth(h);
        }
        else if(arguments.length >= 5){            
            rect.fillStyle(fillStyle);
        }
        else if(arguments.length === 6){            
            rect.strokeStyle(strokeStyle);
        }
    }
})();